package com.luania.todolist2.Activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.luania.todolist2.R;
import com.luania.todolist2.Model.ImageModel;

import java.util.ArrayList;
import java.util.List;

public class AddGroupActivity extends AppCompatActivity {

    List<ImageModel> images = new ArrayList<>();
    ImageView iv_how;
    private EditText et_group_name;
    private String groupName;
    private int choosedIconId = -1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group);

        et_group_name = (EditText) findViewById(R.id.et_group_name);

        Toolbar toolbar1 = (Toolbar) findViewById(R.id.toolbar1);
        toolbar1.setTitle("添加分组");
        toolbar1.setTitleTextColor(getResources().getColor(R.color.colorWhite));
        setSupportActionBar(toolbar1);


        LinearLayout ll_over = (LinearLayout) findViewById(R.id.ll_over);
        ll_over.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AddGroupActivity.this, ListsActivity.class);
                startActivityForResult(intent, 1);
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Intent intent = new Intent();
        iv_how = (ImageView) findViewById(R.id.iv_how);
        intent = data;
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                choosedIconId = intent.getIntExtra("IconId", 0);
                iv_how.setImageResource(choosedIconId);
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main1, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        groupName = et_group_name.getText().toString();
        if (id == R.id.action_add_group1) {

            SharedPreferences sharedPreferences = getSharedPreferences("group", MODE_PRIVATE);
            sharedPreferences.getInt("groupName", -1);//?

            if (choosedIconId == -1) {
                Toast.makeText(AddGroupActivity.this, "图标不能为空！", Toast.LENGTH_SHORT).show();
                return true;
            }
            if (groupName.equals("")) {
                Toast.makeText(AddGroupActivity.this, "分组名不能为空！", Toast.LENGTH_SHORT).show();
                return true;
            }
            if (isExists()) {
                Toast.makeText(AddGroupActivity.this, "该分组名已经存在,请重新输入！", Toast.LENGTH_SHORT).show();
                return true;
            }

            saveGroupInformation();

            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    boolean isExists() {
        groupName = et_group_name.getText().toString();
        SharedPreferences sharedPreferences = getSharedPreferences("group", MODE_PRIVATE);
        return sharedPreferences.contains(groupName);
    }

    void saveGroupInformation() {
        groupName = et_group_name.getText().toString();
        String str = "group";
        SharedPreferences sharedPreferences = getSharedPreferences(str, MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        editor.putInt(groupName, choosedIconId);
        editor.commit();
    }
}
